package com.sanjiahantang.youjiaclient.module.programmeprice.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanjiahantang.plugin.swipemenulistview.NoRollSwipeMenuListView;
import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.MyListView;
import com.sanjiahantang.uilibrary.views.TagGroup;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.config.StaticValue;
import com.sanjiahantang.youjiaclient.module.common.views.impl.WebActivity;
import com.sanjiahantang.youjiaclient.module.programmeprice.beans.ProgramBean;
import com.sanjiahantang.youjiaclient.module.programmeprice.views.ProgramPriceView;
import com.sanjiahantang.youjiaclient.utils.FileUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zy on 2017/8/9.
 */

public class ProgramBeSureAdapter extends RecyclerView.Adapter implements StaticValue {


    private final Context context;
    //布局标识集合
    private final List<Integer> typeList;
    private LayoutInflater inflater;
    private ProgramBean.Data data;
    private Boolean aBoolean = false;
    private BaseView.OnLoadListener loadListener;

    public ProgramBean.Data getData() {
        return data;
    }

    public void setData(ProgramBean.Data data) {
        this.data = data;
    }

    public static final int TYPE_SHIGONG = 2;//施工
    public static final int TYPE_ZHUCAI = 3;
    public static final int TYPE_JIAJU = 4;
    public static final int TYPE_PEISHI = 5;
    private int orderType;//是否是增项

    public ProgramBeSureAdapter(Context context,int type, BaseView.OnLoadListener listener) {
        this.context = context;
        this.typeList = new ArrayList<>();
        typeList.add(TYPE_SHIGONG);
        typeList.add(TYPE_ZHUCAI);
        typeList.add(TYPE_JIAJU);
        typeList.add(TYPE_PEISHI);
        this.loadListener = listener;
        this.inflater = LayoutInflater.from(context);
        orderType = type;
    }

    @Override
    public ProgramHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        int resId = -1;
        switch (viewType) {
            case TYPE_SHIGONG:
                resId = R.layout.item_program_nopic;
                break;
            case TYPE_ZHUCAI:
                resId = R.layout.item_program_nopic_swiplist;
                break;
            case TYPE_JIAJU:
                resId = R.layout.item_program_nopic_swiplist;
                break;
            case TYPE_PEISHI:
                resId = R.layout.item_program_nopic_swiplist;
                break;

        }
        return new ProgramHolder(inflater.inflate(resId, null), data, viewType);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ProgramHolder) holder).bindData(data, typeList.get(position));
    }

    @Override
    public int getItemViewType(int position) {
        return typeList.get(position);
    }

    @Override
    public int getItemCount() {
        return typeList.size();
    }

    private class ProgramHolder extends RecyclerView.ViewHolder {

        public ProgramHolder(View itemView, ProgramBean.Data data, int position) {
            super(itemView);
            bindData(data, position);
        }

        private void bindData(ProgramBean.Data data, int position) {

            if (position == TYPE_SHIGONG) {
                //施工列表
                initService(itemView, data.getServiceList());
            }
            if (position == TYPE_ZHUCAI) {
                //主材列表
                initGoods(itemView,"主材", data.getMaterialList());
            }

            if (position == TYPE_JIAJU) {
                initGoods(itemView,"家具", data.getFurnitureList());
            }

            if (position == TYPE_PEISHI) {
                initGoods(itemView, "配饰",data.getAccessoriesList());
            }
        }

        //施工
        public void initService(View itemView, final List<ProgramBean.Data.ServiceListBean> sgData) {

            final CheckBox sg_checkbox= (CheckBox) itemView.findViewById(R.id.iv_titleIcon);
            final MyListView goodListView = (MyListView) itemView.findViewById(R.id.SG_list);
            TextView txtGoodsCount = (TextView) itemView.findViewById(R.id.comName);
            TextView  TotalCount= (TextView) itemView.findViewById(R.id.TotalCount);
            TextView  TotalPrice= (TextView) itemView.findViewById(R.id.TotalPrice);
            final LinearLayout countItem= (LinearLayout) itemView.findViewById(R.id.countItem);
            sg_checkbox.setVisibility(View.GONE);
            txtGoodsCount.setText("施工");
            float price = 0f;
            for (ProgramBean.Data.ServiceListBean item : sgData) {
                price += Utils.toFloat(item.getServiceCount());
            }
            TotalCount.setText(String.valueOf(sgData.size()));
            TotalPrice.setText(String.format("¥ %.2f" , price));

            if (sgData != null && sgData.size() > 0){
                QuickAdapter SGAdapter = new QuickAdapter<ProgramBean.Data.ServiceListBean>(context, R.layout.item_program_nopic_n, sgData) {
                    @Override
                    protected void convert(BaseAdapterHelper helper, final ProgramBean.Data.ServiceListBean item) {
                        helper.setText(R.id.sg_name, item.getServiceName());
                        helper.setText(R.id.txtPrice, String.format("¥ %.2f" , Utils.toFloat(item.getServiceCount())));
                        helper.getView(R.id.iv_pic).setVisibility(View.GONE);
                        countItem.setVisibility(View.VISIBLE);

                        if (!TextUtils.isEmpty(item.getRemark())){
                            //TODO 2018/3/20 新加施工点击事件
                            helper.setVisible(R.id.iv_arrow,true);
                            helper.getView().setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    WebActivity.open3DUrl(context,item.getRemark(),item.getServiceName());
                                }
                            });
                        }else{
                            helper.getView(R.id.iv_arrow).setVisibility(View.INVISIBLE);
                        }

                        TotalMoney();
                    }

                };
                goodListView.setAdapter(SGAdapter);
            }else if (orderType != ProgramPriceView.TYPE_INCREASE){

                goodListView.setVisibility(View.GONE);
                itemView.findViewById(R.id.panelContract).setVisibility(View.VISIBLE);
                itemView.findViewById(R.id.line).setVisibility(View.GONE);
                itemView.findViewById(R.id.panelContract).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        context.startActivity(new Intent(context, WebActivity.class).putExtra(KEY_TYPE,WEB_TYPE_DISCLAIMER));
                    }
                });
                final CheckBox cbContract = (CheckBox) itemView.findViewById(R.id.Contract);
                cbContract.setChecked(true);
                cbContract.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        cbContract.setChecked(true);
                    }
                });
            }

        }


        //家居
        private void initGoods(View itemView,String title, final List<ProgramBean.Data.GoodsListBean> jjData) {

            final CheckBox JJ_checkbox = (CheckBox) itemView.findViewById(R.id.iv_titleIcon);
            final NoRollSwipeMenuListView JJListView = (NoRollSwipeMenuListView) itemView.findViewById(R.id.SG_list);
            TextView txtGoodsCount = (TextView) itemView.findViewById(R.id.comName);
            final LinearLayout  linearCom = (LinearLayout) itemView.findViewById(R.id.comAdd);
            final TextView line = (TextView) itemView.findViewById(R.id.line);
            TextView  TotalCount= (TextView) itemView.findViewById(R.id.TotalCount);
            TextView  TotalPrice= (TextView) itemView.findViewById(R.id.TotalPrice);
            JJ_checkbox.setVisibility(View.GONE);
            final LinearLayout countItem= (LinearLayout) itemView.findViewById(R.id.countItem);
            int count = 0;
            float price = 0f;
            for (ProgramBean.Data.GoodsListBean item : jjData) {
                count += item.getGoodsNum();
                price += Utils.toFloat(item.getGoodsPrice()) * item.getGoodsNum();
            }

            TotalCount.setText(String.valueOf(count));
            TotalPrice.setText(String.format("¥ %.2f" , price));
            txtGoodsCount.setText(title);

            List<ProgramBean.Data.GoodsListBean> showList = new ArrayList<>();
            showList.clear();
            final QuickAdapter JJAdapter = new QuickAdapter<ProgramBean.Data.GoodsListBean>(context, R.layout.item_program_nopic_y) {
                @Override
                protected void convert(final BaseAdapterHelper helper, final ProgramBean.Data.GoodsListBean item) {
                    helper.setText(R.id.txtMainName, item.getGoodsName());
                    helper.setText(R.id.txtMainPrice,String.format("¥ %.2f" , Utils.toFloat(item.getGoodsPrice())));
                    helper.setText(R.id.txtCount, String.valueOf(item.getGoodsNum()));
                    TagGroup tagGroup = helper.getView(R.id.txtName);
                    tagGroup.setTags(item.getBrandType());
                    ImageView nearby_shop_img = helper.getView(R.id.iv_pic);
                    Picasso.with(context).load(FileUtils.rePath(item.getImageUrl())).placeholder(RES_ID_GOODS_DEFAULT).into(nearby_shop_img);
                    final CheckBox iv_icon = helper.getView(R.id.iv_icon);
                    iv_icon.setVisibility(View.GONE);
                    countItem.setVisibility(View.VISIBLE);
                    TotalMoney();
                }
            };

            final TextView tvMore = (TextView) itemView.findViewById(R.id.tvMore);
            if (jjData.size() > SHOW_SIZE){
                showList.addAll(jjData.subList(0,SHOW_SIZE));
                tvMore.setVisibility(View.VISIBLE);
                tvMore.setTag(R.id.decode,1);
                tvMore.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Object tag = view.getTag(R.id.decode);
                        if (tag != null && tag instanceof Integer){
                            int type = (int) tag;
                            if (type == 1){
                                tvMore.setTag(R.id.decode,2);
                                tvMore.setText("收起列表");
                                JJAdapter.clear();
                                JJAdapter.addAll(jjData);
                            }else{
                                tvMore.setTag(R.id.decode,1);
                                tvMore.setText("查看全部");
                                JJAdapter.clear();
                                JJAdapter.addAll(jjData.subList(0,SHOW_SIZE));
                            }
                        }
                    }
                });
            }else{
                showList.addAll(jjData);
                tvMore.setVisibility(View.GONE);
            }

            JJAdapter.clear();
            JJAdapter.addAll(showList);

            JJListView.setAdapter(JJAdapter);
        }

//
//        private void bindInvoice(View itemView, final CreateOrderPost.OrderInvoiceInfoDtoBean invoice) {
//            if(invoice == null){
//                itemView.findViewById(R.id.rlNoInvoice).setVisibility(View.VISIBLE);
//                itemView.findViewById(R.id.rlInvoiceInfo).setVisibility(View.GONE);
//            }else {
//                itemView.findViewById(R.id.rlNoInvoice).setVisibility(View.GONE);
//                itemView.findViewById(R.id.rlInvoiceInfo).setVisibility(View.VISIBLE);
//
//                TextView txtInvoiceTitle = itemView.findViewById(R.id.txtInvoiceTitle);
//                TextView txtInvoiceTaxNo = itemView.findViewById(R.id.txtInvoiceTaxNo);
//                if (!TextUtils.isEmpty(invoice.getTaxNum())){
//                    itemView.findViewById(R.id.llInvoiceTaxNo).setVisibility(View.VISIBLE);
//                    txtInvoiceTaxNo.setText(invoice.getTaxNum());
//                }else {
//                    itemView.findViewById(R.id.llInvoiceTaxNo).setVisibility(View.GONE);
//                }
//                txtInvoiceTitle.setText(invoice.getInvoiceTitle());
//            }
//
//            if (orderInfo == null || orderInfo.getStatusId() == OrderView.TYPE_UN_PAY){
//                itemView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        Activity activity = (Activity) context;
//                        Intent mIntent = new Intent(activity, InvoiceActivity.class);
//                        if(invoice != null){
//                            if(!TextUtils.isEmpty(invoice.getTaxNum())){
//                                mIntent.putExtra(InvoiceView.KEY_TAX_NO,invoice.getTaxNum());
//                            }
//                            mIntent.putExtra(InvoiceView.KEY_TITLE,invoice.getInvoiceTitle());
//                        }
//                        activity.startActivityForResult(mIntent, OrderView.INTENT_INVOICE);
//                    }
//                });
//                itemView.findViewById(R.id.arrowInvoice1).setVisibility(View.VISIBLE);
//                itemView.findViewById(R.id.arrowInvoice2).setVisibility(View.VISIBLE);
//            }else{
//                itemView.findViewById(R.id.arrowInvoice1).setVisibility(View.GONE);
//                itemView.findViewById(R.id.arrowInvoice2).setVisibility(View.GONE);
//            }
//
//        }
//
//        private void bindConsumerMessage(View itemView, String message) {
//            EditText etConsumerMessage = itemView.findViewById(R.id.etConsumerMessage);
//            if (!TextUtils.isEmpty(message)){
//                etConsumerMessage.setText(message);
//            }
//
//            if (orderInfo != null && orderInfo.getStatusId() != OrderView.TYPE_UN_PAY){
//                etConsumerMessage.setEnabled(false);
//            }
//
//            etConsumerMessage.addTextChangedListener(new TextWatcher() {
//                @Override
//                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//                }
//
//                @Override
//                public void onTextChanged(CharSequence s, int start, int before, int count) {
//                    consumerMessage = s.toString();
//                }
//
//                @Override
//                public void afterTextChanged(Editable s) {
//
//                }
//            });
//        }

    }

    public void TotalMoney() {
        float sgPrice = 0;
        float zcPrice = 0;
        float jjPrice = 0;
        float psPrice = 0;
        //施工总金额
        if (ProgramBeSureAdapter.this.data.getServiceList() != null) {
            for (int i = 0; i < ProgramBeSureAdapter.this.data.getServiceList().size(); i++) {
                if (ProgramBeSureAdapter.this.data.getServiceList().get(i).getIsRecommend() == 1) {
                    sgPrice += Float.parseFloat(ProgramBeSureAdapter.this.data.getServiceList().get(i).getServicePrice()) *
                            Float.valueOf(ProgramBeSureAdapter.this.data.getServiceList().get(i).getServiceNum());
                }
            }
        }
        //资材总金额
        if (ProgramBeSureAdapter.this.data.getMaterialList() != null) {
            for (int i = 0; i < ProgramBeSureAdapter.this.data.getMaterialList().size(); i++) {
                if (ProgramBeSureAdapter.this.data.getMaterialList().get(i).getIsRecommend() == 1) {
                    zcPrice += Float.parseFloat(ProgramBeSureAdapter.this.data.getMaterialList().get(i).getGoodsPrice()) * Float.valueOf(ProgramBeSureAdapter.this.data.getMaterialList().get(i).getGoodsNum());
                }
            }
        }
        //家居总金额
        if (ProgramBeSureAdapter.this.data.getFurnitureList() != null) {
            for (int i = 0; i < ProgramBeSureAdapter.this.data.getFurnitureList().size(); i++) {
                if (ProgramBeSureAdapter.this.data.getFurnitureList().get(i).getIsRecommend() == 1) {
                    jjPrice += Float.parseFloat(ProgramBeSureAdapter.this.data.getFurnitureList().get(i).getGoodsPrice()) * Float.valueOf(ProgramBeSureAdapter.this.data.getFurnitureList().get(i).getGoodsNum());
                }
            }
        }
        //配飾总金额
        if (ProgramBeSureAdapter.this.data.getAccessoriesList() != null) {
            for (int i = 0; i < ProgramBeSureAdapter.this.data.getAccessoriesList().size(); i++) {
                if (ProgramBeSureAdapter.this.data.getAccessoriesList().get(i).getIsRecommend() == 1) {
                    psPrice += Float.parseFloat(ProgramBeSureAdapter.this.data.getAccessoriesList().get(i).getGoodsPrice()) * Float.valueOf(ProgramBeSureAdapter.this.data.getAccessoriesList().get(i).getGoodsNum());
                }
            }
        }
        Object[] objects = new Object[2];
        objects[0] = CALLBACK_TYPE_QUICK_ADAPTER;
        objects[1] = sgPrice + zcPrice + jjPrice + psPrice;
        loadListener.onLoad(objects);
    }

}
